package net.starwifi.duobao.web.member;

import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.entity.Order;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/9/28.
 */
@Controller("apiMemOrderController")
@RequestMapping("/member/order")
public class OrderController extends BaseController {


    @Autowired
    OrderService orderService;

    @Autowired
    MemberService memberService;

    @Autowired
    CartService cartService;

    @Autowired
    ReceiverService receiverService;

    @Autowired
    PaymentMethodService paymentMethodService;

    /**
     * 检查支付
     */
    @ResponseBody
    @RequestMapping(value = "/check_payment", method = RequestMethod.POST)
    public boolean checkPayment(String sn) {
        Order order = orderService.findBySn(sn);
        if (order != null && memberService.getCurrent().getId().equals(order.getMemberId()) && order.getPaymentStatus() == 1) {
            return true;
        }
        return false;
    }


    /**
     * 创建
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody
    Message create(String cartToken, Long receiverId, Long paymentMethodId, @RequestParam(defaultValue = "false") Boolean isInvoice, String invoiceTitle, @RequestParam(defaultValue = "false") Boolean useBalance, String memo) {
        Cart cart = cartService.getCurrent();
        if (cart == null || cart.isEmpty()) {
            return Message.warn("购物车不能为空");
        }
        if (!StringUtils.equals(cart.getToken(), cartToken)) {
            return Message.warn("购物车商品产生了变化");
        }
        if (cart.getIsLowStock()) {
            return Message.warn("购物车已经锁定");
        }
        Receiver receiver = receiverService.find(receiverId);
        if (receiver == null) {
            return Message.error("收货地址不能为空");
        }
        PaymentMethod paymentMethod = paymentMethodService.find(paymentMethodId);
        if (paymentMethod == null) {
            return Message.error("支付方式不能为空");
        }
        Order order = orderService.create(cart, receiver, paymentMethod, null, isInvoice, invoiceTitle, useBalance, memo);
        return Message.success(order.getSn());
    }



    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list(int pageNum) {
        Member member=memberService.getCurrent();
        PageView pageView=new PageView(pageNum,10);
        pageView.addFilter(Filter.eq("memberId", member.getId()));
        pageView.addOrder(net.starwifi.duobao.mvc.Order.desc("id"));
        final Page page=orderService.getScrollData(pageView);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
            put("list",page.getContent());
        }});
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(Model model,Long orderId) {
        net.starwifi.duobao.entity.Order order=orderService.find(orderId);
        Member member=memberService.getCurrent();
        if(!order.getMemberId().equals(member.getId())){
            return ERROR_VIEW;
        }
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        model.addAttribute("order", order);
        return "/member/order/view";
    }

}
